using UnityEngine;
using System.Collections;

public class Pool
{
    public GameObject Prefab;
    public int Cantidad = 10;
    private GameObject[] Objects;
    private int Id = 0;

    [HideInInspector]
    public Vector3 outOfThisWorld;

    public Pool(GameObject prefab, int cantidad)
    {
        this.Prefab = prefab;
        this.Cantidad = cantidad;
        Initialize();
    }
    public GameObject getlast()
    {
        return Objects[Id - 1];
    }

    public void Initialize()
    {
        outOfThisWorld = new Vector3(-1000, -1000, 0);

        Objects = new GameObject[Cantidad];

        for (int i = 0; i < Cantidad; i++)
        {
            Objects[i] = GameObject.Instantiate((Object)Prefab) as GameObject;
            Objects[i].SetActive(false);
        }
    }
    public GameObject Create()
    {
        if (Id < Cantidad)
        {
            GameObject go = Objects[Id];
            go.SetActive(true);
            Id++;
            return go;
        }
        Debug.LogError("Empty pool");
        return null;
    }

    public void Recycle(GameObject go)
    {
        if (Id > 0)
        {
            Id--;
            go.transform.position = outOfThisWorld;
            go.SetActive(false);
            Objects[Id] = go;
        }
        else
        {
            Debug.LogError("No more room for recycling");
        }
    }
    public GameObject[] GetObjects()
    {
        return Objects;
    }
}
